/**
 * @(#) popHouseFundMediator.as
 *
 * @date		2009. 04. 20.
 * @version		1.0
 * @author		Jo, ChangHun
 * @description	연말정산 메인 view - Mediator
 * 				연말정산 - 월세액(주택자금) 공제 팝업창 Logic을 관리 
 */
 package com.skt.common_hr.yearadjustment.view
 {
 	import com.skt.common_hr.common.PsHrAlert;
 	import com.skt.common_hr.common.Variables;
 	import com.skt.common_hr.common.puremvc.CommonMediator;
 	import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
 	import com.skt.common_hr.yearadjustment.model.HouseFundProxy;
 	import com.skt.common_hr.yearadjustment.model.vo.HouseFundVO;
 	import com.skt.common_hr.yearadjustment.view.components.popHouseFund;
 	import com.skt.common_hr.yearadjustment.view.components.popMedicalSubtraction;
 	import com.wcl.utils.DateUtil;
 	
 	import flash.events.Event;
 	import flash.events.MouseEvent;
 	
 	import mx.managers.PopUpManager;
 	
 	import org.puremvc.as3.multicore.interfaces.INotification;
 	
 	
 	public class popHouseFundMediator extends CommonMediator
 	{
 		public static const NAME:String = "popHouseFundMediator";
 		public var v_year:String = "";
		[Bindable] public var v_enable:Boolean = true;
//shgoo 2010.01.12
		//추가, 수정 구분
		private var bAdd:Boolean = true;
				 		
 		/**
 		 * 생성자
 		 * 
 		 * @param
 		 * @return void
 		 * */
 		public function popHouseFundMediator( viewComponent:Object ):void
 		{
 			super(NAME, viewComponent);
 			view.callLater(initView);
 		}
 		
 		/**
 		 * View를 초기화 한다.
 		 * 	- View에 기본사항을 출력하고 data를 load
 		 * @param
 		 * @return void
 		 * */
 		public function initView():void
 		{
 			view.addEventListener(popMedicalSubtraction.OkInput, onOkInput);
 			view.addEventListener(popMedicalSubtraction.CancelInput, onCancelInput);
//shgoo 2010.01.11
 			view.btnAdd.addEventListener(MouseEvent.CLICK, onAdd);
 			view.btnModify.addEventListener(MouseEvent.CLICK, onModify);
/////////////////

 			view._isEnable = v_enable;
 			loadHouseFundData();
 			loadHouse();  			//2014
 		}

 		//2014
 		private function loadHouse():void{
 			var proxy:HouseFundProxy = facade.retrieveProxy(HouseFundProxy.NAME) as HouseFundProxy;
 			proxy.typeload();
 		}
 		 		
//shgoo 2010.01.11 		
 		//추가버튼 클릭 이벤트
 		private function onAdd(event:Event):void {
			bAdd = true;
			/*
 			if(view.RADIO.selectedValue == "X") {	//국세청 자료 총합으로 입력 인경우
				//항목 추가
				view.addArray();
 			}
 			else {	//국세청 자료 총합 입력이 아닌경우
 				//사업자  번호 유효성 체크
 				var proxy:BizNumValidProxy = facade.retrieveProxy(BizNumValidProxy.NAME) as BizNumValidProxy;
 				proxy.load(view.D_COMCD.text);
 			}
 			*/
 		}
 		
 		//수정버튼 클릭 이벤트
 		private function onModify(event:Event):void {
			bAdd = false;
			/*
 			if(view.RADIO.selectedValue == "X") {	//국세청 자료 총합으로 입력 인경우
				//항목 수정
				view.onModifyItem();
 			}
 			else {	//국세청 자료 총합 입력이 아닌경우
 				//사업자  번호 유효성 체크
 				var proxy:BizNumValidProxy = facade.retrieveProxy(BizNumValidProxy.NAME) as BizNumValidProxy;
 				proxy.load(view.D_COMCD.text);
 			}
 			*/
 		}

///////////////////
 		
 		//기본 데이터 조회
 		private function loadHouseFundData():void{
 			var proxy:HouseFundProxy = facade.retrieveProxy(HouseFundProxy.NAME) as HouseFundProxy;
 			proxy.load(Variables.yearPernr, v_year);
 		}
 		
 		//저장
 		private function onOkInput(event:Event):void{
 			
 			var saveList:Array = new Array();
	 		for each(var rec:HouseFundVO in view.houseFundData)
	 		{
	 			var tmp:Object = new Object;
	 			tmp.BEGDA = DateUtil.toBasicString(rec.BEGDA);
	 			tmp.ENDDA = DateUtil.toBasicString(rec.ENDDA);
	 			tmp.BETRG = rec.BETRG;
	 			
	 			//2013
	 			tmp.LDNAM = rec.LDNAM;
	 			tmp.LDREG = String(rec.LDREG).replace(/-/g, '');
	 			tmp.ADDRE = rec.ADDRE;
	 			
	 			//2014
	 			tmp.HOUTY = rec.HOUTY;
	 			tmp.HOUTX = rec.HOUTX;
	 			tmp.FLRAR = rec.FLRAR;	 				 				 			
	 			
	 			saveList.push(tmp);
	 		}
 			
 			var proxy:HouseFundProxy = facade.retrieveProxy(HouseFundProxy.NAME) as HouseFundProxy;
 			proxy.save(Variables.yearPernr, v_year, saveList);
 		}
 		
 		//팝업 닫기
 		private function onCancelInput(event:Event):void
 		{
 			facade.removeMediator(popHouseFundMediator.NAME);
 			PopUpManager.removePopUp(view);
 		}
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					HouseFundProxy.LOAD_HOUSE_FUND_COMPLETE,
					HouseFundProxy.LOAD_HOUSE_FUND_FAILED,
					HouseFundProxy.SAVE_HOUSE_FUND_COMPLETE,
					HouseFundProxy.SAVE_HOUSE_FUND_FAILED,
					HouseFundProxy.LOAD_COMBO_TYPE_HOUSEFUND_COMPLETE
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case HouseFundProxy.LOAD_HOUSE_FUND_COMPLETE:
					view.houseFundData = (note.getBody() as HouseFundProxy).houseFundList;
					break;
				case HouseFundProxy.LOAD_HOUSE_FUND_FAILED:
					trace("YearAdjustmentFacade.LOAD_MEDICAL_LIST_FAILED");
					break;
				case HouseFundProxy.SAVE_HOUSE_FUND_COMPLETE:
					onCancelInput(null);
					//regMediator();
					facade.sendNotification(YearAdjustmentFacade.RELOAD_BASIC_DATA);
					trace("YearAdjustmentFacade.SAVE_DONATION_LIST_COMPLETE:");
					break;
				case HouseFundProxy.SAVE_HOUSE_FUND_FAILED:
					trace("YearAdjustmentFacade.SAVE_MEDICAL_LIST_FAILED");
					PsHrAlert.showMessage((note.getBody() as HouseFundProxy).msgStr);
					break;
					
				case HouseFundProxy.LOAD_COMBO_TYPE_HOUSEFUND_COMPLETE:
					view.comboInType = (note.getBody() as HouseFundProxy).comboInType;
					break;
				default:
								
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function get view():popHouseFund
		{
			return viewComponent as popHouseFund;
		}
	}
 }